﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Data;
//using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Imaging;
//using System.Windows.Shapes;

//namespace CodeStash.Addin.Popups
//{
//    /// <summary>
//    /// Interaction logic for SnippetPopup.xaml
//    /// </summary>
//    public partial class SnippetPopup : Window
//    {
//        public SnippetPopup()
//        {
//            InitializeComponent();
//        }
//    }
//}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Cinch;
using System.ComponentModel.Composition;
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Folding;
using ICSharpCode.AvalonEdit.Highlighting;
using Microsoft.Win32;
using CodeStash.Addin.Helpers;
using CodeStash.Common.DTOS;
using System.Net;


namespace CodeStash.Addin.Popups
{



    [PopupNameToViewLookupKeyMetadata("SnippetPopup", typeof(SnippetPopup))]
    public partial class SnippetPopup : Window
    {

        private FoldingManager foldingManager;
        private AbstractFoldingStrategy foldingStrategy;


        public SnippetPopup()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(SnippetPopup_Loaded);
        }

        void SnippetPopup_Loaded(object sender, RoutedEventArgs e)
        {
            JSONCodeSnippet dc = (JSONCodeSnippet)this.DataContext;
            byte[] a = Encoding.ASCII.GetBytes(WebUtility.HtmlDecode(dc.ActualCode));
            System.IO.MemoryStream m = new System.IO.MemoryStream(a);
            textEditor.Load(m);

            IHighlightingDefinition highlightingDefinition = AvalonEditHelper.Instance.GetHighlightForCodeStashLookup(dc.Language);
            if (highlightingDefinition != null)
            {
                textEditor.SyntaxHighlighting = highlightingDefinition;
                UpdateFoldingStrategy();
            }
        }


        private void UpdateFoldingStrategy()
        {
            if (textEditor.SyntaxHighlighting == null)
            {
                foldingStrategy = null;
            }
            else
            {
                switch (textEditor.SyntaxHighlighting.Name)
                {
                    case "XML":
                        foldingStrategy = new XmlFoldingStrategy();
                        textEditor.TextArea.IndentationStrategy = new ICSharpCode.AvalonEdit.Indentation.DefaultIndentationStrategy();
                        break;
                    case "C#":
                    case "C++":
                    case "PHP":
                    case "Java":
                    case "JavaScript":
                        textEditor.TextArea.IndentationStrategy = new ICSharpCode.AvalonEdit.Indentation.CSharp.CSharpIndentationStrategy(textEditor.Options);
                        foldingStrategy = new BraceFoldingStrategy();
                        break;
                    default:
                        textEditor.TextArea.IndentationStrategy = new ICSharpCode.AvalonEdit.Indentation.DefaultIndentationStrategy();
                        foldingStrategy = null;
                        break;
                }
            }
            if (foldingStrategy != null)
            {
                if (foldingManager == null)
                    foldingManager = FoldingManager.Install(textEditor.TextArea);
                foldingStrategy.UpdateFoldings(foldingManager, textEditor.Document);
            }
            else
            {
                if (foldingManager != null)
                {
                    FoldingManager.Uninstall(foldingManager);
                    foldingManager = null;
                }
            }
        }
    }
}
